// pages/order-create/order-create.js

import api from '../../utils/api'
import store from '../../utils/store'
import message from '../../utils/message';
Page({

    /**
     * 页面的初始数据
     */
    data: {

        action_sheet_hidden:true,
        address:null,
        has_load:false,
        product:null,
        totalPrice:0,
        productList:[],
        bottom_actions:false,
        use_coupou_index:-1,
        coupou:null,
        product_list:[],
        status_url:'',
        realPrice:0.00,
        message:'',
        settle:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        console.log(options);
        this.getOrderPrepare();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        //this.getAddressList();

        const app = getApp();
        this.setData({
            static_url:app.globalData.static_url
        })

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        if(this.data.coupou!=null){

            var realPrice = this.data.totalPrice - this.data.coupou.price;
            this.setData({
                realPrice:realPrice
            })

        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    selectAddress:function(){
        /*
        this.setData({
            action_sheet_hidden:false
        });
        */
        wx.navigateTo({
          url: '/pages/address/address?type=use',
        })

    },
    lisenterActionSheet:function(){
        this.setData({
            action_sheet_hidden:true
        })
    },

    changeMessage:function( e ){
        var value = e.detail.value;
        this.setData({
            message: value
        })
    },


    getAddressList:function(){
        var me = this;
        api.getAddressList(function(response){
            if(response.status==1){
                console.log(response)
                me.setData({
                    addressList: response.data
                })
            }

        })
    },
    addNums:function(){
        var nums = this.data.nums;
        nums++;
        var totalPrice = parseFloat(this.data.product.price * nums).toFixed(2);

        this.setData({
            totalPrice:totalPrice,
            nums:nums
        })
    },
    updatePrice:function(){
        var nums = this.data.nums;
        var totalPrice = parseFloat(this.data.product.price * nums).toFixed(2);
        this.setData({
            totalPrice:totalPrice
        })

    },
    createOrder:function(){
        var me = this;

        if(this.data.address ==null){
            
            wx.showModal({
                title: '提示',
                content: '请选择收获地址',
                showCancel:false,
                success (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
            })
            
            /*
            wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
              })

              */
            return ;
        }

        var obj = {}


        //var arr = JSON.parse( JSON.stringify(this.data.productList) );

        /*
        var product = {}
        product.nums = this.data.nums;
        product.id = this.data.product.id;
        */

        //obj.address = this.data.address;
        //obj.productList = arr;
        //obj.product.push(product)


        var order_data = {};

        if(this.data.coupou==null){
            order_data.coupou_id = -1;
        }else{
            order_data.coupou_id = this.data.coupou.id;  //默认不使用优惠券
        }
        //order_data.coupou_id = this.data.coupou.id;  //默认不使用优惠券
        
        order_data.address_id = this.data.address.id;
        order_data.message = this.data.message;


        api.order.createOrder( order_data , function( result ){
            
            if(result.status==1){
                message.alert( false  ,result.message , "查看订单" , "立即支付" , function( res ){

                    if(res.confirm){
                        me.doOrderPay(result.data.order_no)
                    }
                    if(res.cancel){
                        //跳转到订单详情
                        wx.redirectTo({
                            url: '/pages/order-detail/order-detail?order_no='+result.data.order_no,
                        })
                    }
                });
            }else{
                message.show_error( result.message );
            }
        })

        /*
        var data = JSON.stringify(obj);
        var me = this;
        api.createOrder( { data: data} , function( result ){
            console.log(result);


            if(result.status==1){

                //清空购物车
                store.clear();

                wx.showModal({
                    title:'提示',
                    content:'订单已创建，请尽快支付',
                    cancelText:'查看订单',
                    confirmText:'立即支付',
                    confirmColor:'#3388ff',
                    success:function( res ){
                        if(res.confirm){
                            //跳转到支付

                            wx.navigateTo({
                              url: '/pages/pay/pay?order_no='+result.data.order_no,
                            })

                        }else{
                            //跳转到订单详情

                            wx.navigateTo({
                              url: '/pages/order-detail/order-detail?order_no='+result.data.order_no,
                            })
                        }

                    }
                })


            }
        })

        */

    },
    doOrderPay:function(order_no){
        //var order_no =  e.currentTarget.dataset.no;
        //var order_no = this.data.order_detail.order_no;
          /*
          wx.navigateTo({
            url: '/pages/pay/pay?no='+order_no
          })
    
          */
    
         //var order_no = this.data.order_no;
            
         var me = this;
         api.doPay({order_no:  order_no} , function(result){
             if(result.status==1){
    
                 //console.log(result);
                 me.appletPay( result.data , order_no )
             }else{
                 console.log( result.message )
             }
         });
      },
      appletPay:function( payInfo , order_no ){
        //调用微信支付信息，勾起订单支付
        var me = this;
        wx.requestPayment({
          "timeStamp": payInfo.timeStamp.toString(),
          "nonceStr": payInfo.nonceStr,
          "package": payInfo.package,
          "signType": payInfo.signType,
          "paySign": payInfo.paySign,
          success:function( res ){
              console.log(res);
              //me.getOrderDetail(order_no);
    
              //me.getOrderList(0)


              message.action_success("提示" , "您已成功支付该订单" , "确定" , function(){
                  wx.navigateBack({
                    delta: 1,
                  })
              })
              //console.log(order_no)
          },
          fail:function(res){
              console.log(res)
          }
        })
    
    },
    getOrderPrepare:function () {

        var me = this;

        var params = {};

        if(this.data.coupou!=null){
            params.coupou_id = this.data.coupou.id
        }

        if(this.data.address!=null){
            params.address_id = this.data.address.id
        }

        api.order.getOrderPrepare( params , function (response) {

            console.log(response);

            if(response.status==1){
                me.setData({
                    product_list:response.data.product_list,
                    address: response.data.address,
                    settle:response.data.settle,
                    coupou:response.data.coupou
                })
            }else{

            }
            
        })
        
    }

})